<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>发布新闻</title>
  <style>
    body {
      padding-left: 300px;
    }
    .item {
      margin: 20px;
    }
  </style>
</head>
<body>
  <form>
    <div class="item">
      标题
      <input type="text" name="title" placeholder="标题">
    </div>
    <div class="item">
      内容
      <textarea name="content" cols="30" rows="10" placeholder="内容"></textarea>
    </div>
    <div class="item">
      作者
      <input type="text" name="author" placeholder="作者">
    </div>
    <div class="item">
      标签
      <input type="text" name="tags" placeholder="标签">
    </div>
    <div class="item">
      配图
      <input type="file" name="picture">
    </div>
    <div class="item">
      <button>发布</button>
    </div>
  </form>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script>
    const form = document.querySelector('form')
    form.addEventListener('submit', function(e) {
      e.preventDefault()
      const fd = new FormData(this)
      axios({
        url: 'http://42.192.45.224:3007/api/addNews',
        method: 'POST',
        headers: {
          'Content-Type': 'multipart/form-data'
        },
        data: fd
      }).then(({ data }) => {
        if (data.status !== 0) return alert(data.message)
        alert('文章发布成功')
        form.reset()
      })
    })
  </script>
</body>
</html>
